home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: PLEASE PLEASE...THANKS THANKS!!
- Date: 1 Jan 1996 15:25:34 GMT
- Organization: The Internet Access Group, Inc.
- Message-ID: <4c8ude$lut@news.iag.net>
- References: <1995Dec31.061049.16566@wvnvms>
- NNTP-Posting-Host: pm2-orl21.iag.net
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.6
-
- In article <1995Dec31.061049.16566@wvnvms>, un025043@wvnvms.wvnet.edu says...
- >
- >Thanks a lot for that stuff on state machines!
- >As two of you pointed out, using fork or spawn is an alternative,
- >nut that makes it os-dependent.
- >Also, as you very wisely remarked, it is not possible to control
- >the numnber of instrcutions executed, but only the timeslice.
- >
- >The only choice left seemed to be that of using a state machine model,
- >having static ints in each procedure & using as many "cases" for the switch
- >as the number of statements.
- >But you see, the solution is quite not as simple...
- >if I had 4 STATEMENTS, i could intersperse them with four switch cases,
- >but since I have a LOOP, where do I put the switch cases?
- >I mean, you can't expand a conditional loop and interleave each statement
- >with case statements, right?
- >
- >If you can tackle this minor(major) hitch, please let me know.
- >I shall continue to think on the lines of the "state model".
-
- Can you fake the loop by using one or more additional static ints for
- iteration control? Something like:
-
- #include <stdio.h>
- #include <stdlib.h>
-
- int func1( void)
- {
- static int step=0, iter=0;
- switch( step)
- {
- case 0:
- puts( "func1, step0");
- step++;
- break;
-
- case 1:
- if( iter != 4)
- printf( "func1, step1, iter%d\n", iter++);
- else
- {
- step++;
- iter=0;
- }
- break;
-
- case 2:
- puts( "func1, step2");
- step++;
- break;
-
- default:
- return 0;
- }
- return 1;
- }
-
- int func2( void)
- {
- static int step=0, iter=0;
- switch( step)
- {
- case 0:
- puts( "func2, step0");
- step++;
- break;
-
- case 1:
- if( iter != 2)
- printf( "func2, step1, iter%d\n", iter++);
- else
- {
- step++;
- iter=0;
- }
- break;
-
- case 2:
- puts( "func2, step2");
- step++;
- break;
-
- default:
- return 0;
- }
- return 1;
- }
-
-
- int main()
- {
- int f1stat=1, f2stat=1;
-
- while( f1stat || f2stat)
- {
- if( f1stat)
- f1stat = func1();
- if( f2stat)
- f2stat = func2();
- }
- return 0;
- }
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-